#ifndef WORDLIB_H
#define WORDLIB_H

#include "_Global/Sqlite/Sqlite.h"

#include <QList>

struct _EnglishWord
{
    qint32  m_nID;     //单词id
    QString m_strWord;
    QString m_strPhonetic;
    QString m_strExplain; //解释
    //qint16 myAnswer;  //用户的答案,-1没有答题,0为错误,1为正确;貌似没用？？？
    //bool used;      //该局已使用过

    explicit _EnglishWord()
    {
        m_nID = -1;
        //myAnswer = -1;
    }
};

//暴露给QML的 1个单词 的对象
class _WordObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString word READ word)
    Q_PROPERTY(QString phonetic READ phonetic)
    Q_PROPERTY(QStringList explain READ explain)
    Q_PROPERTY(qint16 answer READ answer)
    //上面4个对应_EnglishWord

    Q_PROPERTY(qint16 answerChoice READ answerChoice WRITE setAnswerChoice) //用户答案,-1为没选择
    Q_PROPERTY(qint16 choiceTime READ choiceTime WRITE setChoiceTime) //用户答案,-1为没选择
    //Q_ENUMS(showType)
public:
    /*
    enum _ShowType
    {
        show_Choice = 0,
        show_Only = 1
    };*/

    explicit _WordObject(QObject *parent = nullptr)
        :QObject(parent)
    {
        m_pWord = nullptr;

        m_nAnswer = -1;
        m_nAnswerChoice = -1;

    }
    virtual ~_WordObject()
    {
    }

    QString word() const
    {
        return m_pWord->m_strWord;
    }
    QString phonetic() const
    {
        return m_pWord->m_strPhonetic;
    }
    QStringList explain() const
    {
        return m_listExplain;
    }
    qint16 answer() const
    {
        return m_nAnswer;
    }
    qint16 answerChoice() const
    {
        return m_nAnswerChoice;
    }
    void setAnswerChoice(qint16 &an)
    {
        m_nAnswerChoice = an;
    }
    qreal choiceTime() const
    {
        return m_fChoiceTime;
    }
    void setChoiceTime(qreal an)
    {
        m_fChoiceTime = an;
    }
public:
    _EnglishWord *m_pWord;      //指向的Word指针
    _EnglishWord m_Word;       //自带一个Word结构体(网络游戏时用,赋值给m_pWord)
    //_ShowType eShowType;

    qint16      m_nAnswer;          //正确答案下标:0~4
    qint16      m_nAnswerChoice;    //其他人选择下标，-1为没选择
    qreal       m_fChoiceTime;       //算则使用的时间比

    QStringList m_listExplain;  //答案序列
    //QList<int> m_ExplainIdList; //
};

class WordLib : public QObject, public Sqlite
{
    Q_OBJECT
    Q_PROPERTY(int wordCount READ wordCount)    //一个课程中单词总数
    Q_PROPERTY(int correctCount READ correctCount)  //正确数
    Q_PROPERTY(int mistakeCount READ mistakeCount)  //错误数
    Q_PROPERTY(int unStudiedCount READ unStudiedCount)  //没学习的数
    Q_PROPERTY(int currentWordCount READ currentWordCount)  //本局完成的单词数
    Q_PROPERTY(int currentCorrectCount READ currentCorrectCount)    //本局正确的
    Q_PROPERTY(int currentMistakeCount READ currentMistakeCount)    //....错误的
    Q_PROPERTY(int currentUnStudiedCount READ currentUnStudiedCount)    //本局没学习的
    Q_PROPERTY(int totalScore READ totalScore)  //本局总分数

    Q_PROPERTY(int fastChoice READ fastChoice)    //快速选择个数
    Q_PROPERTY(int continualChoice READ continualChoice)        //持续选对次数
    Q_PROPERTY(int continualStatistics READ continualStatistics)  //持续选对统计

    Q_PROPERTY(int scoreCorrect READ scoreCorrect)        //一个单词分数
    Q_PROPERTY(int scoreMistake READ scoreMistake)        //一个单词分数
    Q_PROPERTY(int scoreWin READ scoreWin)        //一个单词分数
    Q_PROPERTY(int scoreFastChoice READ scoreFastChoice)        //一个单词分数
    Q_PROPERTY(int scoreTerminate READ scoreTerminate)        //一个单词分数

public:
    explicit WordLib(QObject *parent = nullptr);
    virtual ~WordLib();

    void init();    //初始化结果信息

public: //暂时用不到
    //_EnglishWord *createNewWord();      //创建新单词
    //void appendWord(_EnglishWord *pWord)    //追加一个单词到m_listWord尾部
    //{
    //    m_listWord.append(pWord);
    //}

    //先暂时用不到这个函数
    //bool getAnswersCount(int &correct, int &error);

    void Debug(QString msg);

public:
    //从数据库读取单词到内存(所有classID,levelID的单词)
    bool createWords(int classID, int levelID);

    //得到一个单词信息到pWordObj
    //userWord:是否是用户的单词(true则返回下一个要学的单词,false则随机抽取一个供电脑)
    //返回false表示出错,或者单词全部学完
    bool getWord(_WordObject* pWordObj, bool userWord);

    //创建一个单词信息到pWordObj(网络游戏用)
    //wordsid为4个单词id,word为正确下标
    //返回true提取成功,否则false
    bool createWord(_WordObject* pWordObj, int correctID, int wordsID[]);



    //跳到结果中为'u'的单词(没学),学完返回false
    bool moveToNextWord();



    //r为空,则表示没学,将结果初始化为'u'(未学习);
    bool setResult(QString r);

    //创建结果
    //QString createResult();
    void deleteAll();               //从内存中删除全部单词

    //返回等于classID,levelID的单词数
    int getWordCount(int classID, int levelID);


public:
    /*
    QString wordLevelID() const
    {
        return wordLevelID;
    }*/
    int wordCount() const
    {
        return m_listWord.count();
    }
//保存结果前
    int mistakeCount() const
    {
        int n = 0;
        QString::const_iterator ii;
        for(ii = m_strResult.constBegin(); ii != m_strResult.constEnd(); ii++)
            if(*ii == 'n')n++;
        return n;
    }
    int correctCount() const
    {
        int n = 0;
        QString::const_iterator ii;
        for(ii = m_strResult.constBegin(); ii != m_strResult.constEnd(); ii++)
            if(*ii == 'y')n++;
        return n;
    }
    int unStudiedCount() const
    {
        int n = 0;
        QString::const_iterator ii;
        for(ii = m_strResult.constBegin(); ii != m_strResult.constEnd(); ii++)
            if(*ii == 'u')n++;
        return n;
    }

//目前已回答单词中
    int currentWordCount() const
    {
        return m_nWordCount;
    }
    int currentMistakeCount() const
    {
        return m_nMistakeCount;
    }
    int currentCorrectCount() const
    {
        return m_nCorrectCount;
    }
    int currentUnStudiedCount() const
    {
        return m_nWordCount - m_nMistakeCount - m_nCorrectCount;
    }

    int totalScore() const
    {
        return m_nCorrectCount * scoreCorrect()
                + m_nMistakeCount * scoreMistake()
                + m_nFastChoice * scoreFastChoice()
                + m_nContinualStatistics;
    }

    int fastChoice() const
    {
        return m_nFastChoice;
    }

    int continualChoice() const
    {
        return m_nContinualChoice;
    }

    int continualStatistics() const
    {
        return m_nContinualStatistics;
    }

    int scoreCorrect() const
    {
        return 20;
    }
    int scoreMistake() const
    {
        return -10;
    }
    int scoreWin() const
    {
        return 10;
    }
    int scoreFastChoice() const
    {
        return 2;
    }
    int scoreTerminate() const
    {
        return -100;
    }


public:
    int m_nWordCount;        //本局!!! 已回答的单词数
    int m_nCorrectCount;     //本局!!! 已回答的正确的单词
    int m_nMistakeCount;     //本局!!! 已回答错误....

    int m_nFastChoice;  //快速选择个数
    int m_nContinualChoice;     //持续选对次数
    int m_nContinualStatistics; //持续选对统计



    int m_nType;         //学习模式.1:英译汉;2:汉译英
    int m_nClassID;
    int m_nLevelID;

    int m_nStudyType;        //1:学习;2:复习.影响提取的单词(u和n)

    QString m_strResult;         //学习结果
    int m_nWordPointer;       //单词指针(当前单词下标)
    //QString bakResult;      //备份学习结果,是否需要更新服务端

    int *m_pnComputerWords;      //电脑要考的单词顺序数组
    int m_nComputerWordPointer; //下标

private:
    //QList<_EnglishWord*>::ConstIterator wordPointer;

    QList<_EnglishWord*> m_listWord;  //单词数组
};

/*
 * 鹰:
 * 单人游戏时,所有的函数和数据都可使用
 *
 * 网络游戏时:
 * createWord(),m_nWordCount,m_nCorrectCount,m_nMistakeCount
 * currentXXX(),totalScore()
 * 可用
*/

#endif // WORDLIB_H
